/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jtmcraft.graphics;

import java.awt.Image;
import java.util.EnumMap;

/**
 *
 * @author tom
 */
public class Sprite {

    public enum eSpriteAction
    {
        none,
        moveN,
        moveNE,
        moveE,
        moveSE,
        moveS,
        moveSW,
        moveW,
        moveWN,
        attackN,
        attackNE,
        attackE,
        attackSE,
        attackS,
        attackSW,
        attackW,
        attackWN,
    };

    private EnumMap< eSpriteAction, Image[] > images = new EnumMap<eSpriteAction, Image[]>( Sprite.eSpriteAction.class);
    private eSpriteAction spriteAction = null;
    private boolean move = false;
    private int imageNr = 0;
    private int period = 50; //ms
    private long lastTimeStamp = 0;

    public Sprite() {
    }


    public void AddSpriteImages( eSpriteAction spriteAction, ImageResourceFactory.eImageResource imageResource )
    {
        ImageResourceFactory imageResourceFactory = ImageResourceFactory.getInstance();
        Image[] img = imageResourceFactory.getImageSequence( imageResource );
        this.spriteAction = spriteAction;
        images.put(spriteAction, img );
    }

    public void AddSpriteImages( ImageResourceFactory.eImageResource imageResource )
    {
        AddSpriteImages( eSpriteAction.none, imageResource );
    }

    public int getImageCount()
    {
        return images.get( spriteAction ).length;
    }

    public Image getImage()
    {
        if( getImageCount() > 0 )
        {
            imageNr %= getImageCount();
        }
        return images.get( spriteAction )[imageNr];
    }

    public void process( long timeStamp )
    {
        if( move )
        {
            if( timeStamp > lastTimeStamp + period )
            {
                long delta = timeStamp - lastTimeStamp;
                lastTimeStamp = timeStamp;

                int count =(int) delta / period;
                //System.out.println(count);
                imageNr += count;
            }
        } else
        {
            lastTimeStamp = timeStamp;
        }
    }

    public void processNext()
    {
        if( move )
        {
          imageNr++;
        }
    }

    public eSpriteAction getSpriteAction() {
        return spriteAction;
    }

    public void setSpriteAction(eSpriteAction spriteAction) {
        this.spriteAction = spriteAction;
    }

    public boolean isMove() {
        return move;
    }

    public void setMove(boolean move) {
        this.move = move;
    }

}
